package au.edu.adelaide.mips_soft_core.board_interface_pc.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import au.edu.adelaide.mips_soft_core.board_interface_pc.core.Core;
import au.edu.adelaide.mips_soft_core.board_interface_pc.core.CoreListener;
import au.edu.adelaide.mips_soft_core.board_interface_pc.core.Device;
import au.edu.adelaide.mips_soft_core.board_interface_pc.core.DeviceListener;

public class InfoPanel extends JPanel implements CoreListener, DeviceListener{
	
	JLabel[] cpuLabels = new JLabel[16];
	JLabel[] cpuFrequencies = new JLabel[16];
	
	JLabel[] memoryLabels = new JLabel[16];
	JLabel[] memorySize = new JLabel[16];
	
	
	Device connectedDevice = null;
	
	public InfoPanel(){
		super(new GridBagLayout());
		addComponent(this, new JLabel("CPUs"), 0, 0, 2, 1);
		for(int i = 0; i < 16; i++){
			cpuLabels[i] = new JLabel("CPU #" + i + " Frequency (Hz)");
			cpuFrequencies[i] = new JLabel(Integer.toString((i+1) * 1000));
			addComponent(this, cpuLabels[i], 0, i + 1, 1, 1);
			addComponent(this, cpuFrequencies[i], 1, i + 1, 1, 1);
		}
		addComponent(this, new JLabel("Memory Modules"), 0, 17, 2, 1);
		for(int i = 0; i < 16; i++){
			memoryLabels[i] = new JLabel("Memory Module #" + i + " Size (32bit word)");
			memorySize[i] = new JLabel(Integer.toString((i+1) * 100));
			addComponent(this, memoryLabels[i], 0, i + 18, 1, 1);
			addComponent(this, memorySize[i], 1, i + 18, 1, 1);
		}
		updatePanel();
		Core.getCore().addCoreListener(this);
	}
	
	void updatePanel(){
		{
			for(int i = 0; i < 16; i++){
				cpuLabels[i].setVisible(false);
				cpuFrequencies[i].setVisible(false);
			}
			for(int i = 0; i < 16; i++){
				memoryLabels[i].setVisible(false);
				memorySize[i].setVisible(false);
			}
		}
		if(connectedDevice != null){
			for(int i = 0; i < connectedDevice.getNumberOfCPUModules(); i++){
				cpuLabels[i].setVisible(true);
				cpuFrequencies[i].setText(Integer.toString(Device.lookUpFrequencyTableValue((byte)connectedDevice.getFrequencyOfCPUModule((byte)i))));
				cpuFrequencies[i].setVisible(true);
			}
			for(int i = 0; i < connectedDevice.getNumberOfMemoryModules(); i++){
				memoryLabels[i].setVisible(true);
				memorySize[i].setText(Integer.toString(connectedDevice.getSizeOfMemoryModule((byte)i)));
				memorySize[i].setVisible(true);
			}
		}
	}
	
	
	
	private void addComponent(JPanel panel, JComponent component, int x, int y, int width, int height){
		 GridBagConstraints c = new GridBagConstraints();
		 c.gridx = x;
		 c.gridy = y;
		 c.gridheight = height;
		 c.gridwidth = width;
		 panel.add(component,  c);
	}

	@Override
	public void error(String message) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void boardDiagnosed() {
		updatePanel();		
	}


	@Override
	public void deviceListChanged() {}

	@Override
	public void deviceConnected(Device device) {
		this.connectedDevice = device;
		device.addDeviceListener(this);
		
	}

	@Override
	public void deviceDisconnected(Device device) {
		this.connectedDevice = null;
		device.removeListener(this);
		updatePanel();
		
	}

	@Override
	public void modeChanged() {}

}
